#!/usr/bin/python
# -*- coding: UTF-8 -*-

from kpautos.engine.case import Case
from kptest.config.enum import DeviceType, HostType, DeviceUtility


class IPMCSET_IPINFO_001(Case):
    """
            Name:
                IPMCSET_IPINFO_001
            Label:
                查询BMC管理网口的IP信息
            PreCondition:
                BMC正常启动
            TestStep:
                1) 使用MobaXterm工具ssh连接BMC，在命令行输入，ipmcget -d ipinfo，查询BMC管理网口的IP信息，预期结果A
            ExpectedResult:
                A) 获取到的查询BMC管理网口的IP信息与web端网络配置，ipv4和ipv6，与实际一致
            Parameters:

            Author:
                baixin liuya
            Last Modified:

            Remark:

        """

    # 测试执行前配置等准备操作
    def pre_test_case(self):
        self.logger.info("Pre Test Case Start.... ")
        self.device_dut = self.resource.get_device(device_type=DeviceType.Server, utility=DeviceUtility.DUT)
        self.bmc_host = self.device_dut.get_host(host_type=HostType.BMC)

    # 测试步骤
    def procedure(self):
        self.log_step("Step1: 通过ipmcset执行查询ipinfo信息")
        resp = self.bmc_host.run({"command": ["ipmcget -d ipinfo"], "timeout": 10})
        rc = resp['rc']
        if rc != 0:
            self.assertIsNotNone(None, "BMC控制台执行[ipmcget -d ipinfo]命令失败")
        stdout = resp['stdout']
        ipv4 = None
        ipv6 = None
        stdout_list = stdout.split('\n')
        for buf in stdout_list:
            if 'ip address' in buf.lower() and 'Backup' not in buf:
                ipv4 = buf.split(':')[-1]
            if 'ipv6 address' in buf.lower():
                ipv6 = buf.split(':')[-1]

        self.log_step(f"Step2: 查询BMC管理网口的IP信息：\nIPv4: {ipv4}\nIPv6: {ipv6}")

    # 测试结束后恢复环境
    def post_test_case(self):
        pass